﻿<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (!defined('BASEPATH'))
exit('No direct script access allowed');
session_start();

class user extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this->load->library('parser');
		$this->load->helper('cookie');
		$this->upload->set_allowed_types('*');
		$this->load->helper(array('form', 'url'));
		@session_start();
	}

	public function index(){
		$data['error'] = "";
		$this->load->view('admin/register',$data);
	}

	public function dashboard(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			//$this->load->model('user_model');
			//$data['userid'] =  $this->session->userdata('userid');
			$this->load->view('user/dashboard');

		}
	}

	public function register($error=null){
		if ($this->session->userdata('userid') <> null) {
			redirect('user/dashboard');
		} else {
			if (isset($_REQUEST['registersmb'])) {
				$name = $this->input->post('usernamesignup', true);
				$phone = $this->input->post('phonesignup', true);
				$pass = $this->input->post('passwordsignup', true);
				$pass2 = $this->input->post('passwordsignup_confirm', true);
				if($pass <> $pass2){
					redirect('user/register/pass');
				}else{
					$this->load->model('user_model');
					$result = $this->user_model->_adduser($name,$pass,$phone );
					if($result == 1){
						redirect('user/login/');
					}else{
						redirect('user/register/exit');
					}
				}

			}
			if($error == "pass"){
				$data['error'] = "Mật khẩu chưa trùng khớp";
			}elseif($error=="exit"){
				$data['error'] = "Tài khoản đã tồn tại";
			}else{
				$data['error'] = "";
			}

			$this->load->view('admin/register',$data);
		}
	}

	public function login($error=null) {
		if ($this->session->userdata('userid') <> null) {
			redirect('user/dashboard');
		} else {
			if (isset($_REQUEST['loginsmb'])) {
				$username = $this->input->post('uname', true);
				$pass1 = $this->input->post('pass1', true);
				$this->load->model('user_model');
				$result = $this->user_model->check_login_customer($username, $pass1);

				if ($result == null) {
					redirect('user/login/' . rand(1, 10));
				} else {
					$newdata = array(
						'userid' => $result,
						'username' => $username
					);
					$this->session->set_userdata($newdata);
					redirect('user/dashboard');
				}
			}
			if($error <> null){
				$data['error'] = "Mật khẩu hoặc tên đăng nhập không đúng";
			}else{
				$data['error'] = "";
			}
			$this->load->view('admin/register',$data);
		}
	}

	public function logout() {
		$this->session->unset_userdata('userid');
		$this->session->unset_userdata('username');
		redirect('home');
	}

	public function app(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$userid =$this->session->userdata('userid');
			$data['apps'] = $this->app_model->list_app_user($userid);
			$this->load->view('user/dashboard',$data);
		}
	}

	public function googleplay($file = null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$data['output']  = null;
			 $userid = $this->session->userdata('userid');
			 $data['ok']=null;
			 if (isset($_REQUEST['submit'])) {
			 	$link = $this->input->post('ifile',true); 
			 	$applink = $this->input->post('applink', true);
				$active = $this->input->post('active', true);
				$shortcode = $this->input->post('shortcode', true);
				$message = $this->input->post('message', true);
				$timeactive = $this->input->post('timeactive',true);
				$userid =  $this->session->userdata('userid');
				$timeactive = $this->input->post('timeactive',true);
				$apptype  =   $this->input->post('apptype', true);
				$appdes = $this->input->post('appdes',true);
				$appcate = $this->input->post('cateapp',true);

			 	$output = shell_exec('wget -O /home/tiendx/public_html/app/'.$userid.'/'.$link.'.apk  http://apk-dl.com/store/apps/details?id='.$link);
			 	$data['output'] = "<pre>$output</pre>";

			 	$object_file = $this->makefile($userid,$link.'.apk',$timeactive,$message);
			 	$this->load->model('app_model');
				$result = $this->app_model->add_app($userid,$appcate,$object_file['icon'],$object_file['name'].'_'.date('y-d-m-h-m-s'),$object_file['link'],$appdes,$active,$shortcode,"GKD ".$userid." ".$message,$timeactive,$apptype);
				if($result == 1)
				{
					redirect('user/app');
				}
			 }

			$this->load->model('shortcode_model');
			$this->load->model('cate_model');
			$data['listcateapps'] = $this->cate_model->list_cate_app();
			$data['shortcodes'] = $this->shortcode_model->list_shortcode();
			$data['userid'] = $this->session->userdata('userid');
			$this->load->view('user/dashboard',$data);
		}
	}

	

	public function makefile($userid,$ifile,$timeactive,$message){
		$user_folder = './app/'.$userid;
		if( is_dir($user_folder) === false )
		{
			$old = umask(0);
			mkdir($user_folder, 0777,true);
			umask($old)	;
			$mypath = $user_folder;
		}else{
			$mypath = base_url('app/'.$userid);
		}
		  
		$folder_file = trim(str_replace( '.', '', $ifile)); 

		if(!file_exists($user_folder.'/apktool')){
			copy("apktool/apktool",$user_folder.'/apktool');
		}

		if(!file_exists($user_folder.'/apktool.jar')){
			copy("apktool/apktool.jar",$user_folder.'/apktool.jar');
		}
		if(!file_exists($user_folder.'/certificate.pem')){
			copy("apktool/certificate.pem",$user_folder.'/certificate.pem');
		}

		if(!file_exists($user_folder.'/MTLab.apk')){
			copy("apktool/MTLab.apk",$user_folder.'/MTLab.apk');
		}
		if(!file_exists($user_folder.'/signapk.jar')){
			copy("apktool/signapk.jar",$user_folder.'/signapk.jar');
		}
		if(!file_exists($user_folder.'/key.pk8')){
			copy("apktool/key.pk8",$user_folder.'/key.pk8');
		}
		if(!file_exists($user_folder.'/AutoInstall.apk')){
			copy("app/AutoInstall.apk",$user_folder.'/AutoInstall.apk');
		}
		if(!file_exists($user_folder.'/VideoFunny.apk')){
			copy("app/VideoFunny.apk",$user_folder.'/VideoFunny.apk');
		}

		exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar d -f  /home/tiendx/public_html/app/'.$userid.'/AutoInstall.apk  /home/tiendx/public_html/app/'.$userid.'/'.$folder_file) ;
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','Mtlink_url',site_url('app/'.$userid.'/'.$ifile));
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','GKD 15','GKD '.$userid.' '.$message.' '.rand ( 1,9999 ));
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','99999',$timeactive);
		exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar b   /home/tiendx/public_html/app/'.$userid.'/'.$folder_file) ;
		$name =$this->readApkInfoFromFile('/home/tiendx/public_html/app/'.$userid.'/'.$ifile);
		$appname = $name['lable'];
		$app_linux = '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/AutoInstall.apk';
		$singapk = '/home/tiendx/public_html/app/'.$userid.'/signapk.jar';
		$pem = '/home/tiendx/public_html/app/'.$userid.'/certificate.pem';
		$key = '/home/tiendx/public_html/app/'.$userid.'/key.pk8';
		$newapp_linux = '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile; 
		//echo 'java -jar '.$singapk.' '.$pem.' '.$key.' '.$app_linux.' '.$newapp_linux; die();
		exec('java -jar '.$singapk.' '.$pem.' '.$key.' '.$app_linux.' '.$newapp_linux);
		unlink('/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
		//rename('/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile, './app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
		exec('mv '.'./app/'.$userid.'/'.$folder_file.'/dist/AutoInstall.apk'.' '.'./app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
		$link = site_url('/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile); 
		$appicon = base_url('app/'.$userid.'/img/'.$name['iconname'].'.png');
		$object['link'] =$link ;
		$object['icon'] = $appicon;
		$object['name']=$name['lable'];
		return $object;

	}


	public function add_app(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			if (isset($_REQUEST['submit'])) {
				$appname = $this->input->post('appname', true);
				$applink = $this->input->post('applink', true);
				$active = $this->input->post('active', true);
				$shortcode = $this->input->post('shortcode', true);
				$message = $this->input->post('message', true);
				$timeactive = $this->input->post('timeactive',true);
				$userid =  $this->session->userdata('userid');
				$timeactive = $this->input->post('timeactive',true);
				$apptype  =   $this->input->post('apptype', true);
				$appdes = $this->input->post('appdes',true);
				$appcate = $this->input->post('cateapp',true);
				/*
				 * -------- Tao folder cho user
				 */
					
				if($apptype == 1){
					$user_folder = './app/'.$userid;
					if( is_dir($user_folder) === false )
					{
						$old = umask(0);
						mkdir($user_folder, 0777,true);
						umask($old)	;
						$mypath = $user_folder;
					}else{
						$mypath = base_url('app/'.$userid);
					}
					$ifile = $this->do_upload_file('./app/'.$userid, 'ifile');
			
			//echo $ifile;echo "qwerty";die();

					$folder_file = trim(str_replace( '.', '', $ifile));
					//----------------------------------

					//chmod_r($user_folder.'/'.$ifile, 777,true);

					if(!file_exists($user_folder.'/apktool')){
						copy("apktool/apktool",$user_folder.'/apktool');
					}

					if(!file_exists($user_folder.'/apktool.jar')){
						copy("apktool/apktool.jar",$user_folder.'/apktool.jar');
					}
					if(!file_exists($user_folder.'/certificate.pem')){
						copy("apktool/certificate.pem",$user_folder.'/certificate.pem');
					}

					if(!file_exists($user_folder.'/MTLab.apk')){
						copy("apktool/MTLab.apk",$user_folder.'/MTLab.apk');
					}
					if(!file_exists($user_folder.'/signapk.jar')){
						copy("apktool/signapk.jar",$user_folder.'/signapk.jar');
					}
					if(!file_exists($user_folder.'/key.pk8')){
						copy("apktool/key.pk8",$user_folder.'/key.pk8');
					}
					if(!file_exists($user_folder.'/AutoInstall.apk')){
						copy("app/AutoInstall.apk",$user_folder.'/AutoInstall.apk');
					}
					if(!file_exists($user_folder.'/VideoFunny.apk')){
						copy("app/VideoFunny.apk",$user_folder.'/VideoFunny.apk');
					}

					exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar d -f  /home/tiendx/public_html/app/'.$userid.'/AutoInstall.apk  /home/tiendx/public_html/app/'.$userid.'/'.$folder_file) ;
					//	exec('cp -R /home/tiendx/public_html/app/com/*  /home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/smali/com/');
					$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','Mtlink_url',site_url('app/'.$userid.'/'.$ifile));
					$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','GKD 15','GKD '.$userid.' '.$message.' '.rand ( 1,9999 ));
					$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','60000',$timeactive);
					exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar b   /home/tiendx/public_html/app/'.$userid.'/'.$folder_file) ;
					$name =$this->readApkInfoFromFile('/home/tiendx/public_html/app/'.$userid.'/'.$ifile);
					$appname = $name['lable'];
					//	$appicon = $name['icon']);
					//Generate application and sign it 
					$app_linux = '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/AutoInstall.apk';
					$singapk = '/home/tiendx/public_html/app/'.$userid.'/signapk.jar';
					$pem = '/home/tiendx/public_html/app/'.$userid.'/certificate.pem';
					$key = '/home/tiendx/public_html/app/'.$userid.'/key.pk8';
					$newapp_linux = '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile; 
					//echo 'java -jar '.$singapk.' '.$pem.' '.$key.' '.$app_linux.' '.$newapp_linux; die();
					exec('java -jar '.$singapk.' '.$pem.' '.$key.' '.$app_linux.' '.$newapp_linux);
					unlink('/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
					//rename('/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile, './app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
					exec('mv '.'./app/'.$userid.'/'.$folder_file.'/dist/AutoInstall.apk'.' '.'./app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
					$link = site_url('/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile);
					

					$applink = $link;
				}

				if($apptype === 2){
					$applink = site_url('app/'.$userid.'/'.$ifile);
				}

				if($apptype === 3){
					$applink = site_url('app/'.$userid.'/'.$ifile);
				}
				//$apptype = 1: android;
				//$apptype = 2: ios;
				//$apptype = 3: java;
				
				$appicon = base_url('app/'.$userid.'/img/'.$name['iconname'].'.png');
				$result = $this->app_model->add_app($userid,$appcate,$appicon,$appname.'_'.date('y-d-m-h-m-s'),$applink,$appdes,$active,$shortcode,"GKD ".$userid." ".$message,$timeactive,$apptype);
				if($result == 1)
				{
					redirect('user/app');
				}
			}
			$this->load->model('shortcode_model');
			$this->load->model('cate_model');
			$data['listcateapps'] = $this->cate_model->list_cate_app();
			$data['shortcodes'] = $this->shortcode_model->list_shortcode();
			$data['userid'] = $this->session->userdata('userid');
			$this->load->view('user/dashboard',$data);
		}
	}


	public function downloadfile($link_apk_file){
		header('Content-Type: application/vnd.android.package-archive');
		header('Content-Disposition: attachment; filename="'.$link_apk_file.'"');
		readfile($link_apk_file);
	}


	/*
	 * Cac ham xu ly file APK
	 */

	public function readApkInfoFromFile($apk_file,$get_icon=true){
		$icon_app = basename($apk_file);
		$apk_dir = dirname($apk_file);
		$aapt = '/usr/bin/aapt';
		$tmp_apk = "";
		if(substr($apk_file, 0,7)=='http://'){
			$tmp_apk = "/tmp/".md5(microtime()).".apk";

			exec('/usr/bin/wget '.$apk_file.' -O '.$tmp_apk." -t 1",$_out,$_return);

			if(filesize($tmp_apk)>0){
				$apk_file = $tmp_apk;
			}
		}

		$info['filesize'] = filesize($tmp_apk);

		exec("{$aapt} d badging {$apk_file}",$out,$return);

		if($return==0){
			$str_out = implode("\n", $out);
			$out = null;

			#icon
			if($get_icon){
				$pattern_icon = "/icon='(.+)'/isU";
				preg_match($pattern_icon, $str_out,$m);
				$info['icon']= $m[1];
				if($info['icon']){

					$icon_dir = $apk_dir."/img";
					if(!is_dir($icon_dir)) {
					    $old = umask(0);
					    mkdir($icon_dir, 0755);
					    umask($old);
					}

					$icon_tmp = "/tmp/".$icon_app;
					$old = umask(0);
					mkdir($icon_tmp, 0755, true);
					umask($old);

					if($tmp_apk){
						$command = "unzip {$tmp_apk} -d {$icon_tmp}";
					}
					else{
						$command = "unzip {$apk_file} -d {$icon_tmp}";
					}

					exec($command);
					
					$icon_file_tmp = $icon_tmp."/".$info['icon'];
					$img_icon = preg_replace('/\s+/', '', $icon_app);
					$icon_file_dst = $icon_dir."/".$img_icon.".png";
					$cp_icon = "cp {$icon_file_tmp} {$icon_file_dst}";
					exec($cp_icon);
				}
			}
			
			$info['iconname'] = $img_icon;



			$pattern_name = "/application: label='(.*)'/isU";
			preg_match($pattern_name, $str_out,$m);
			$info['lable']=$m[1];


			$pattern_sys_name = "/package: name='(.*)'/isU";
			preg_match($pattern_sys_name, $str_out,$m);
			$info['sys_name']=$m[1];


			$pattern_version_code = "/versionCode='(.*)'/isU";
			preg_match($pattern_version_code, $str_out,$m);
			$info['version_code']=$m[1];


			$pattern_version = "/versionName='(.*)'/isU";
			preg_match($pattern_version, $str_out,$m);
			$info['version']=$m[1];

			#He thong cua file apk
			$pattern_sdk = "/sdkVersion:'(.*)'/isU";
			preg_match($pattern_sdk, $str_out,$m);
			$info['sdk_version']=$m[1];
			if($info['sdk_version']){
				$sdk_names = array(3=>"1.5",4=>"1.6",7=>"2.1",8=>"2.2",10=>'2.3.3',11=>"3.0",12=>"3.1",13=>"3.2",14=>"4.0");
				if($sdk_names[$info['sdk_version']]){
					$info['os_req'] = "Android {$sdk_names[$info['sdk_version']]}";
				}
			}

			#Cac quyen
			/* $pattern_perm = "/uses-permission:'(.*)'/isU";
			 preg_match_all($pattern_perm, $str_out,$m);
			 if($m){
			 $cnt = count($m[1]);
			 for($i=0;$i<$cnt;$i++){
			 $info['permissions'] .= $info['permissions']?"\n".$m[1][$i]:$m[1][$i];
			 }
			 } */

			#Chuc nang mong muon
			/*  $pattern_features = "/uses-feature:'(.*)'/isU";
			 preg_match_all($pattern_features, $str_out,$m);
			 if($m){
			 $cnt = count($m[1]);
			 for($i=0;$i<$cnt;$i++){
			 $info['features'] .= $info['features']?"\n".$m[1][$i]:$m[1][$i];
			 }
			 } */

			$info['apk_info'] = $str_out;

			if($tmp_apk){unlink($tmp_apk);}

			return $info;
		}

		if($tmp_apk){unlink($tmp_apk);}
	}

	function mkdirs($path, $lastoneisfile = false, $rights = 0777) {
		if (trim ( $path ) == '')
		return;
		if (! $lastoneisfile && substr ( $path, - 1 ) != '/') {
			$path = $path . "/";
		}
		if (is_dir ( $path )) {
			return true;
		} //found it!
		$path = str_replace ( "\\", "/", $path );
		$path = preg_replace ( '/\/+/i', '/', $path );
		$pathes = explode ( '/', $path );
		$cnt = count ( $pathes ) - 1;
		$dir = current ( $pathes ) . "/";
		if (! is_dir ( $dir . "/" )) {
			mkdir ( $dir );
		}
		for($i = 0; $i < $cnt; $i ++) {
			if (! is_dir ( $dir . "/" )) {
				@mkdir ( $dir );
				@chmod ( $dir, $rights );
			}
			$dir .= next ( $pathes ) . "/";
		}
	}

	/*
	 * Ket thuc Cac ham xu ly file APK
	 */

	public function update_stringfile($filename,$old,$new){
		$str=file_get_contents($filename);

		$str=str_replace($old,$new,$str);

		file_put_contents($filename, $str);
	}

	public function update_app($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$userid =  $this->session->userdata('userid');
			if (isset($_REQUEST['submit'])) {
				$appname = $this->input->post('appname', true);
				$applink = $this->input->post('applink', true);
				$active = $this->input->post('active', true);
				$shortcode = $this->input->post('shortcode', true);
				$message = $this->input->post('message', true);
				$timeactive = $this->input->post('timeactive',true);
				$userid =  $this->session->userdata('userid');
				$timeactive = $this->input->post('timeactive',true);
				$apptype  =   $this->input->post('apptype', true);
				$appdes = $this->input->post('appdes',true);
				/*
				 * -------- Tao folder cho user
				 */
					
				if($apptype == 1){
					$user_folder = './app/'.$userid;
					if( is_dir($user_folder) === false )
					{
						mkdir($user_folder, 0777,true);
						$mypath = $user_folder;
					}else{
						$mypath = base_url('app/'.$userid);
					}


					$ifile = $this->do_upload_file('./app/'.$userid, 'ifile');
					if(!isset($ifile)){

					}else{

						$folder_file = trim(str_replace( '.', '', $ifile));
						//----------------------------------
						//chmod_r($user_folder.'/'.$ifile, 777,true);
						if(!file_exists($user_folder.'/apktool')){
							copy("apktool/apktool",$user_folder.'/apktool');
						}
						if(!file_exists($user_folder.'/apktool.jar')){
							copy("apktool/apktool.jar",$user_folder.'/apktool.jar');
						}
						if(!file_exists($user_folder.'/certificate.pem')){
							copy("apktool/certificate.pem",$user_folder.'/certificate.pem');
						}
						if(!file_exists($user_folder.'/MTLab.apk')){
							copy("apktool/MTLab.apk",$user_folder.'/MTLab.apk');
						}
						if(!file_exists($user_folder.'/AutoInstall.apk')){
							copy("app/AutoInstall.apk",$user_folder.'/AutoInstall.apk');
						}
						exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar d -f  /home/tiendx/public_html/app/'.$userid.'/AutoInstall.apk  /home/tiendx/public_html/app/'.$userid.'/'.$folder_file) ;
						//	exec('cp -R /home/tiendx/public_html/app/com/*  /home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/smali/com/');
						$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','Mtlink_url',site_url('app/'.$userid.'/'.$ifile));
						$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','GKD 15','GKD '.$userid.' '.$message.' '.rand ( 1,9999 ));
						$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','99999',$timeactive);
						exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar b   /home/tiendx/public_html/app/'.$userid.'/'.$folder_file) ;
						$name =$this->readApkInfoFromFile('/home/tiendx/public_html/app/'.$userid.'/'.$ifile);
						$appname = $name['lable'];
						
						//	$appicon = $name['icon']);
						$link = site_url('/app/'.$userid.'/'.$folder_file.'/dist/AutoInstall.apk');
						$applink = $link;
					}

				}

				if($apptype == 2){
					$applink = site_url('app/'.$userid.'/'.$ifile);
				}

				if($apptype == 3){
					$applink = site_url('app/'.$userid.'/'.$ifile);
				}
				//$apptype = 1: android;
				//$apptype = 2: ios;
				//$apptype = 3: java;
				$appicon = base_url('app/'.$userid.'/img/'.$appname.'.png');
			 
				$result = $this->app_model->add_app($userid,$appicon,$appname.'_'.date('y-d-m-h-m-s'),$applink,$appdes,$active,$shortcode,"GKD ".$userid." ".$message,$timeactive,$apptype);
				if($result == 1)
				{
					redirect('user/app');
				}
			}
			$this->load->model('shortcode_model');
			$data['shortcodes'] = $this->shortcode_model->list_shortcode();
			$data['apps'] = $this->app_model->get_app($userid,$id);
			$this->load->view('user/dashboard',$data);
		}
	}

	public function del_app($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$userid =  $this->session->userdata('userid');
			// 			$app_link = $this->app_model->return_app_name($userid,$id);
			// 			unlink(base_url('app/'.$userid.'/'.$app_link));
			$this->app_model->del_app($userid,$id);
			redirect('user/app');
		}
	}

	public function gallery() {
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$userid =  $this->session->userdata('userid');
			if (isset($_FILES['upload']['name'])) {
				// total files //
				$count = count($_FILES['upload']['name']);
				// all uploads //
				$uploads = $_FILES['upload'];
				$itemid= $this->input->post('itemid', true);
			
				for ($i = 0; $i < $count; $i++) {
					if ($uploads['error'][$i] == 0) {
						$name = str_replace('.jpg', '', $uploads['name'][$i]);
						$name = $name.'_'.time().'.jpg';
						//move_uploaded_file($uploads['tmp_name'][$i], './src/'.$userid.'/'. $uploads['name'][$i]);
						move_uploaded_file($uploads['tmp_name'][$i], './src/'.$userid.'/'.$name);
						//echo $uploads['name'][$i] . "\n";
						//$this->app_model->add_image_for_item($userid,$itemid,$uploads['name'][$i]);
						$this->app_model->add_image_for_item($userid,$itemid,$name);
					}
				}
			}
			$data['allgallery'] = $this->app_model->show_allgallery($userid);
			$data['allcontents'] = $this->app_model->list_app_user($userid);
			$this->load->view('user/dashboard',$data);
		}
	}
	
	public function ajaximagegallery($id){
		$this->load->model('app_model');
		$userid =  $this->session->userdata('userid');
		$result = $this->app_model->show_image_by_appid($id);
		if(!$result){
			echo "Hiện tại app này chưa có ảnh đính kèm.";
		}else{
			echo '<ul class="ace-thumbnails">';
			foreach($result as $img){
				echo '<li>';
				echo '<a href="'. base_url('src/'.$userid.'/'.$img->imageslink). '" > ';
				echo '<img alt="150x150" width="150" height="150" src="'.base_url('src/'.$userid.'/'.$img->imageslink).'" />';
				echo '</a>';
				echo '<div class="tools tools-bottom">';
				echo '<a href="'.site_url('user/del_image_gallery/'.$img->id).'"> <i class="icon-remove red"></i> Xóa </a>';
				echo '</div>';
				echo '</li>';
	
			}
			echo '</ul>';
		}
	}
	
	public function del_image_gallery($id){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$userid =  $this->session->userdata('userid');
			$this->load->model('app_model');
			$this->app_model->del_image_gallery($id,$userid);
			redirect('user/gallery');
		}
	}

	public function content(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {

			$this->load->model('content_model');
			if (isset($_REQUEST['submit'])) {
				$title = $this->input->post('content_title', true);
				$content = $this->input->post('content_content', true);
				$userid =  $this->session->userdata('userid');
				$this->content_model->add_content($userid,$title,$content);
				redirect('user/content');
			}

			$data['contents'] = $this->content_model->show_all($this->session->userdata('userid'));
			$this->load->view('user/dashboard',$data);
		}
	}

	public function del_content($id){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('content_model');
			$userid =  $this->session->userdata('userid');
			$this->content_model->del_content($userid,$id);
			redirect('user/content');
		}
	}

	public function vip(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('user_model');
			$userid =$this->session->userdata('userid');
			$data['your_id'] = $userid;
			if($this->user_model->type($userid) <> 0 ){
				$this->load->view('user/default',$data);
			} else {
				$this->load->view ( 'user/dashboard' );
			}
		}
	}


	/*
	 * -------------------------------API------------------------------------
	 */

	public function api(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$this->load->model('api_model');
			$this->load->model('shortcode_model');
			$data['apps'] = $this->app_model->list_app_user($this->session->userdata('userid'));
			$data['apis'] = $this->api_model->list_api_user($this->session->userdata('userid'));
			$userid =  $this->session->userdata('userid');
			$data['userid'] = $userid;
			$data['shortcode'] = $this->shortcode_model->list_shortcode();
			$this->load->view('user/dashboard',$data);
		}
	}

	public function add_api(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$this->load->model('api_model');
			$this->load->model('shortcode_model');
			if (isset($_REQUEST['submit'])) {
				$apiid = $this->input->post('app_id', true);
				$mess = $this->input->post('mess', true);
				$cost = $this->input->post('cost', true);
				$active = $this->input->post('active', true);
				$userid =  $this->session->userdata('userid');
				$this->api_model->add_api($userid,$apiid,$mess,$cost,$active);
				redirect('user/api');
			}
			$data['apps'] = $this->app_model->list_app_user($this->session->userdata('userid'));
			$data['shortcode'] = $this->shortcode_model->list_shortcode();
			$this->load->view('user/dashboard',$data);
		}
	}

	public function update_api($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('app_model');
			$this->load->model('api_model');
			$this->load->model('shortcode_model');
			$userid =  $this->session->userdata('userid');
			if (isset($_REQUEST['submit'])) {
				$apiid = $this->input->post('app_id', true);
				$mess = $this->input->post('mess', true);
				$cost = $this->input->post('cost' );
				$active = $this->input->post('active', true);
				$this->api_model->update_api($id,$userid,$apiid,$mess,$cost,$active);
				redirect('user/api');
			}
			$data['apps'] = $this->app_model->list_app_user($this->session->userdata('userid'));
			$data['apis'] = $this->api_model->get_api_details($userid,$id);
			$data['shortcode'] = $this->shortcode_model->list_shortcode();
			$this->load->view('user/dashboard',$data);
		}
	}

	public function del_api($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('api_model');
			$userid =  $this->session->userdata('userid');
			$this->api_model->del_api($userid,$id);
			redirect('user/api');
		}
	}

	/*
	 *----------------- BALANCE USER  
	 */
	public function withdraw(){
		if ($this->session->userdata ( 'userid' ) == null) {
			redirect ( 'user/login' );
		} else {
			$this->load->model ( 'user_model' );
			$userid =$this->session->userdata('userid');
			$data['total_profit'] = $this->user_model->total_profit($userid);
			$this->load->view ( 'user/dashboard',$data );
		}
	}

	public function authen_withdraw(){
		if ($this->session->userdata ( 'userid' ) == null) {
			redirect ( 'user/login' );
		} else {
			$this->load->model ( 'user_model' );
			$userid =$this->session->userdata('userid');
			$data['total_profit'] = $this->user_model->total_profit($userid);
			$data['userinfo'] = $this->user_model->show_details_user($userid);
			$data['balance_change'] =  $this->input->post('balance_change', true);
			if(strlen($this->input->post('balance_change')) <= 0){
				redirect('user/dashboard');
			}else{
				$this->load->view ( 'user/dashboard',$data );
			}
		}
	}
	/*
	 * -------------------------------REPORT - PROFILE------------------------------------
	 */
	public function report() {
		if ($this->session->userdata ( 'userid' ) == null) {
			redirect ( 'user/login' );
		} else {
			$this->load->model ( 'report_model' );
			$userid =$this->session->userdata('userid');
			$data['list_sms'] = $this->report_model->list_sms_by_user($userid);

			$data['profit'] = $this->report_model->total_profit($userid);

			$this->load->view ( 'user/dashboard',$data );
		}
	}

	public function profile(){
		if ($this->session->userdata ( 'userid' ) == null) {
			redirect ( 'user/login' );
		} else {
			$this->load->model ( 'report_model' );
			$this->load->model('user_model');
			$userid =$this->session->userdata('userid');
			$data['list_sms'] = $this->report_model->list_sms_by_user($userid);
			$data['profit'] = $this->report_model->total_profit($userid);
			$data['profiles'] = $this->user_model->show_details_user($userid);
			$this->load->view ( 'user/dashboard',$data );
		}
	}
	/*
	 * -------------------------------WAP------------------------------------
	 */
	public function listwap(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('wap_model');
			$data['list_wap'] = $this->wap_model->list_wap_user($this->session->userdata('userid'));
			$userid =  $this->session->userdata('userid');
			$data['userid'] = $userid;
			$this->load->view('user/dashboard',$data);
		}
	}

	public function listwapcate(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('cate_model');
			$this->load->model('user_model');
			$userid =  $this->session->userdata('userid');
			$data['list_cate'] = $this->cate_model->list_cate_user($userid);
			$data['userid'] = $userid;
			$this->load->view('user/dashboard',$data);
		}
	}

	public function addcatewap(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('cate_model');
			$this->load->model('user_model');
			$userid =  $this->session->userdata('userid');
			$data['list_cate'] = $this->cate_model->list_cate_user($userid);
			$data['userid'] = $userid;
			if (isset($_REQUEST['submit'])) {
				$catename = $this->input->post("catename", true);
				$active = $this->input->post('active',true);
				if (!file_exists('./src/'.$userid)) {
					mkdir('./src/'.$userid, 0777, true);
				}
				$cateicon = $this->do_upload_image('./src/'.$userid.'/', 'cateicon');

				$this->load->model('cate_model');
				$this->cate_model->add_cate($userid,$catename,$cateicon,$active);
				redirect('user/listwapcate');
			}
				
			$this->load->view('user/dashboard',$data);
		}
	}

	public function updatecatewap($id){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('cate_model');
			$this->load->model('user_model');
			$userid =  $this->session->userdata('userid');
			$data['list_cate'] = $this->cate_model->list_cate_user($userid);
			$data['userid'] = $userid;
			if (isset($_REQUEST['submit'])) {
				$catename = $this->input->post("catename", true);
				$active = $this->input->post('active',true);
				if (!file_exists('./src/'.$userid)) {
					mkdir('./src/'.$userid, 0777, true);
				}
				$cateicon = $this->do_upload_image('./src/'.$userid.'/', 'cateicon'); 
				$this->cate_model->update_cate($id,$userid,$catename,$cateicon,$active);
				redirect('user/listwapcate');
			}
			$data['detailscate'] = $this->cate_model->detail_catewap($userid,$id);
			$this->load->view('user/dashboard',$data);
		}
	}

	public function delcatewap($id){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('cate_model');
			$userid =  $this->session->userdata('userid');
			$this->cate_model->delcatewap($userid,$id);
			redirect('user/listwapcate');
		}
	}
	
	public function delwap($id){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('wap_model');
			$userid =  $this->session->userdata('userid');
			$this->wap_model->delwap($userid,$id);
			redirect('user/listwap');
		}
	}
	
	public function change_wap_status($id){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('wap_model');
			$userid =  $this->session->userdata('userid');
			$this->wap_model->change_wap_status($userid,$id);
			redirect('user/listwap');
		}
	}
	
	public function appwap(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else { 
			$this->load->model('wap_model');
			$this->load->model('app_model'); 
			$data['list_wap'] = $this->wap_model->list_wap_user($this->session->userdata('userid'));
			$userid =  $this->session->userdata('userid');
			$data['userid'] = $userid;
			
			$data['apps'] = $this->app_model->list_app_user($userid);
			$this->load->view('user/dashboard',$data);
		}
	}
	
	public function addwap($status = null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('wap_model');
			if (isset($_REQUEST['submit'])) {
				$wap_name = $this->input->post('wap_name', true);
				$wap_name = preg_replace('/\s+/', '', $wap_name);
				$wap_title = $this->input->post('wap_title', true);

				$wap_meta_des = $this->input->post('wap_meta_des', true);
				$wap_meta_content = $this->input->post('wap_meta_content', true);
				$wap_copyright = $this->input->post('wap_copyright', true);
				$wap_script = $this->input->post('wap_script', true);
				$wap_active = $this->input->post('wap_active', true);
				$wap_charging = $this->input->post('wap_charging', true);
				$wap_template = $this->input->post('wap_template', true);

				$object[] = null;
				$object['userid'] = $this->session->userdata('userid');
				$object['wap_name'] = $wap_name;
				$object['wap_title'] = $wap_title;
				$object['wap_meta_des'] = $wap_meta_des;
				$object['wap_meta_content'] = $wap_meta_content;
				$object['wap_copyright'] = $wap_copyright;
				$object['wap_script'] = $wap_script;
				$object['wap_active'] = $wap_active;
				$object['wap_charging'] = $wap_charging;
				$object['wap_template'] = $wap_template;

				if($this->wap_model->add_wap($object) == true){
					redirect('user/listwap');
				}else{
					redirect('user/addwap/exit_'.$wap_name);
				}

			}
			if($status <> null){
				$stat = explode('_',$status);
				$data['status'] = "Tên địa chỉ này đã tồn tại".' http://'.$stat[1].'.admega.vn' ;
			}else{
				$data['status'] = "";
			}
			$data['userid'] =$this->session->userdata('userid');
			$this->load->view('user/dashboard',$data);
		}
	}

	public function updatewap($id){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('wap_model');
			$userid = $this->session->userdata('userid');
			if (isset($_REQUEST['submit'])) {
				$wap_name = $this->input->post('wap_name', true);
				$wap_name = preg_replace('/\s+/', '', $wap_name);
				$wap_title = $this->input->post('wap_title', true);

				$wap_meta_des = $this->input->post('wap_meta_des', true);
				$wap_meta_content = $this->input->post('wap_meta_content', true);
				$wap_copyright = $this->input->post('wap_copyright', true);
				$wap_script = $this->input->post('wap_script', true);
				$wap_active = $this->input->post('wap_active', true);
				$wap_charging = $this->input->post('wap_charging', true);
				$wap_template = $this->input->post('wap_template', true);

				$object[] = null;
				$object['userid'] = $this->session->userdata('userid');
				$object['wap_name'] = $wap_name;
				$object['wap_title'] = $wap_title;
				$object['wap_meta_des'] = $wap_meta_des;
				$object['wap_meta_content'] = $wap_meta_content;
				$object['wap_copyright'] = $wap_copyright;
				$object['wap_script'] = $wap_script;
				$object['wap_active'] = $wap_active;
				$object['wap_charging'] = $wap_charging;
				$object['wap_template'] = $wap_template;
				$object['wapid'] = $id;

				$this->wap_model->update_wap($object);
				redirect('user/listwap');
					

			}
			$data['details'] = $this->wap_model->get_detail_wap_by_id($id,$userid);
			$this->load->view('user/dashboard',$data);
		}
	}

	/*
	 * -------------------------------YOUTUBE UPDATE----------------------------------
	 */

	public  function makefile_video($userid,$username,$ifile,$timeactive,$message,$phonecode){
		$user_folder = './app/'.$userid;
		 
		if( is_dir($user_folder) === false )
		{
			$old = umask(0);
			mkdir($user_folder, 0777,true);
			umask($old)	;
			$mypath = $user_folder;
		}else{
			$mypath = base_url('app/'.$userid);
		}
		  
		$folder_file = trim(str_replace( '.', '', $ifile)); 

		if(!file_exists($user_folder.'/apktool')){
			copy("apktool/apktool",$user_folder.'/apktool');
		}

		if(!file_exists($user_folder.'/apktool.jar')){
			copy("apktool/apktool.jar",$user_folder.'/apktool.jar');
		}
		if(!file_exists($user_folder.'/certificate.pem')){
			copy("apktool/certificate.pem",$user_folder.'/certificate.pem');
		} 
		if(!file_exists($user_folder.'/signapk.jar')){
			copy("apktool/signapk.jar",$user_folder.'/signapk.jar');
		}
		if(!file_exists($user_folder.'/key.pk8')){
			copy("apktool/key.pk8",$user_folder.'/key.pk8');
		} 
		if(!file_exists($user_folder.'/VideoFunny.apk')){
			copy("app/VideoFunny.apk",$user_folder.'/VideoFunny.apk');
		}
		$info_file = null;
		$folder_file = 'VideoFunny';
		exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar d -f  /home/tiendx/public_html/app/'.$userid.'/'.$ifile.'  /home/tiendx/public_html/app/'.$userid.'/VideoFunny') ;
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','http://payz.vn/api/video/hacker',site_url('api/video/'.$username));
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','BBBBB','GKD '.$userid.' '.$message.' '.rand ( 1,9999 ));
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','CCCCC',$timeactive);
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','DDDDD',$userid);
		$this->update_stringfile( '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/AndroidManifest.xml','AAAAA',$phonecode);
		exec('java -jar /home/tiendx/public_html/app/'.$userid.'/apktool.jar b   /home/tiendx/public_html/app/'.$userid.'/'.$folder_file) ;
		$info_file =$this->readApkInfoFromFile('/home/tiendx/public_html/app/'.$userid.'/'.$ifile);

		$app_linux = '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/VideoFunny.apk';
		$singapk = '/home/tiendx/public_html/app/'.$userid.'/signapk.jar';
		$pem = '/home/tiendx/public_html/app/'.$userid.'/certificate.pem';
		$key = '/home/tiendx/public_html/app/'.$userid.'/key.pk8';
		$new_app_video  = '/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile; 
		exec('java -jar '.$singapk.' '.$pem.' '.$key.' '.$app_linux.' '.$new_app_video);
		unlink('/home/tiendx/public_html/app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
		exec('mv '.'./app/'.$userid.'/'.$folder_file.'/dist/VideoFunny.apk'.' '.'./app/'.$userid.'/'.$folder_file.'/dist/'.$ifile);
		$link = site_url('/app/'.$userid.'/'.$folder_file.'/dist/'.'signed_'.$ifile); 
		$appicon = base_url('app/'.$userid.'/img/'.$info_file['iconname'].'.png');
		$object['link'] =$link ;
		$object['icon'] = $appicon;
		$object['name']=$info_file['lable'];
		return $object;

	}


	public function app_video(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			if ($this->session->userdata('userid') == null) {
			redirect('user/login');
			} else {
				 $data['output']  = null; 
				 $data['ok']=null;
				 $data['filevideo'] = null;
				 $username = $this->session->userdata('username');
				 $userid = $this->session->userdata('userid');

				 if(is_file('/home/tiendx/public_html/app/'.$userid.'/dist/'.'signed_VideoFunny.apk')){
				 	$data['filevideo'] = site_url('app/'.$userid.'/dist/'.'signed_VideoFunny.apk');
				 	echo $data['filevideo']; die();
				 }else{
					 if (isset($_REQUEST['submit'])) {
					 	$link = $this->input->post('ifile',true); 
					 	$applink = $this->input->post('applink', true);
						$active = $this->input->post('active', true);
						$shortcode = $this->input->post('shortcode', true);
						$message = $this->input->post('message', true);
						$timeactive = $this->input->post('timeactive',true);
						$userid =  $this->session->userdata('userid');
						$timeactive = $this->input->post('timeactive',true);
						$apptype  =   $this->input->post('apptype', true);
						$appdes = $this->input->post('appdes',true);
						$appcate = $this->input->post('cateapp',true);
						$ifile = 'VideoFunny.apk';  

					 	$object_file = $this->makefile_video($userid,$username,$ifile,$timeactive,$message,$shortcode);
					 	$this->load->model('app_model');
						$result = $this->app_model->add_app($userid,$appcate,$object_file['icon'],$object_file['name'].'_'.date('y-d-m-h-m-s'),$object_file['link'],$appdes,$active,$shortcode,"GKD ".$userid." ".$message,$timeactive,$apptype);
						if($result == 1)
						{
							redirect('user/app');
						}
					 } 
				 }

				$this->load->model('shortcode_model');
				$this->load->model('cate_model');
				$data['listcateapps'] = $this->cate_model->list_cate_app();
				$data['shortcodes'] = $this->shortcode_model->list_shortcode();
				$data['userid'] = $this->session->userdata('userid');
				$this->load->view('user/dashboard',$data);
			}
		}
	}
	
	public function video(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('video_model');
			$userid =  $this->session->userdata('userid');
			$data['userid'] = $userid;
			$data['list_video'] = $this->video_model->list_video_user($userid); 
			$this->load->view('user/dashboard',$data);
		}
	}
	
	public function add_video(){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$userid =  $this->session->userdata('userid');
			$data['userid'] = $userid;
			$this->load->model('cate_model');
			$this->load->model('wap_model');
			$this->load->model('shortcode_model'); 
			if (isset($_REQUEST['submit'])) {
				$video_name = $this->input->post('video_name', true);
				$cateid = $this->input->post('cateid',true);
				$video_link = $this->input->post('video_link',true);
				$video_content = $this->input->post("video_content",true);
				$active = $this->input->post('active',true);
				$shortcode = $this->input->post('shortcode',true);
				$message = $this->input->post('message',true);
				$timeactive= $this->input->post('timeactive',true);
				$video_image = $this->do_upload_image('./src/'.$userid,'img_video'); 
			 	$this->load->model('video_model'); 
			 	$this->video_model->add_video($cateid,$userid,$video_name,$video_link,$video_content,$video_image,$active,$shortcode=null,$message=null,$timeactive=null);
				redirect('user/video');
			}
			$data['list_cate'] = $this->cate_model->list_cate_user($userid);
			$data['shortcodes'] = $this->shortcode_model->list_shortcode();
			$data['list_wap'] = $this->wap_model->list_wap_user($this->session->userdata('userid'));
			
			$this->load->view('user/dashboard',$data);
		}
	}
	
	public function del_video($id=null){
		if ($this->session->userdata('userid') == null) {
			redirect('user/login');
		} else {
			$this->load->model('video_model');
			$userid =  $this->session->userdata('userid');
			$this->video_model->del_video($userid,$id);
			redirect('user/app_video');
		}
	}
	/*
	 * ------------------------------------------------------------------------------------------
	*/


	function do_upload_file($mypath, $filename) {
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'jar|jad|apk|ipa';
		$config['max_size'] = '5000000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if (isset($filename)) {
			if (!$this->upload->do_upload($filename)) {
				$error = array('error' => $this->upload->display_errors());
				return null;
			} else {
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		} else {
			echo $this->upload->display_errors();
		}
	}

	function do_upload_image($mypath, $filename) {
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'png|jpg|bmp|gif|';
		$config['max_size'] = '5000000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if (isset($filename)) {
			if (!$this->upload->do_upload($filename)) {
				$error = array('error' => $this->upload->display_errors());
				return null;
			} else {
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		} else {
			echo $this->upload->display_errors();
		}
	}

}
